package com.wangsan.study.web;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangsan.study.domain.RoleEntity;
import com.wangsan.study.domain.RoleEntityRepository;

/**
 * UserEntityController
 *
 * @author wangsan
 * @date 2016/12/28
 */
@Controller
@RequestMapping(value = "/role")
public class RoleEntityController {

    @Autowired
    private RoleEntityRepository roleEntityRepository;

    @RequestMapping("/list")
    public void list(Model model) {
        Iterable<RoleEntity> userEntityIterable = roleEntityRepository.findAll();
        model.addAttribute("entityList", userEntityIterable);
    }

    @RequestMapping("/add")
    public void add(RoleEntity entity, Model model) {
        model.addAttribute("entity", entity);
    }

    @RequestMapping(value = "/save")
    public String save(@Valid RoleEntity entity, BindingResult result, Model model) {
        roleEntityRepository.save(entity);
        return "redirect:/role/list";
    }

    @RequestMapping("/{id}")
    @ResponseBody
    public RoleEntity view(@PathVariable("id") Long id) {
        return roleEntityRepository.findOne(id);
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        roleEntityRepository.delete(id);
        return "redirect:/role/list";
    }
}
